package com.tianlu.store.controller;

import com.tianlu.store.common.IdGen;
import com.tianlu.store.common.ResponseResult;
import com.tianlu.store.common.ResultCode;
import com.tianlu.store.entity.UserAddress;
import com.tianlu.store.service.UserAddressService;
import com.tianlu.store.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author 梁东烂
 * @Desc 收件地址表(UserAddress)表控制层
 * @Date 2023-04-03 17:32:45
 */
@Api(tags = "收件地址表")
@RestController
@RequestMapping("/api/userAddress")
@RequiredArgsConstructor
public class UserAddressController {
    @Resource
    private UserAddressService userAddressService;

    @ApiOperation("收件地址表列表(不分页）接口")
    @PostMapping("/findList")
    public ResponseResult<List<UserAddress>> findList(@RequestBody UserAddress userAddress) {
        userAddress.setUserId(JwtUtils.getIdByJwtToken());
        return ResponseResult.success(userAddressService.findList(userAddress));
    }

    @ApiOperation("添加收件地址表接口")
    @PostMapping("/save")
    public ResponseResult<Boolean> save(@RequestBody UserAddress userAddress) {
        userAddress.setId(IdGen.snowflakeId());
        userAddress.setUserId(JwtUtils.getIdByJwtToken());
        return ResponseResult.success(userAddressService.save(userAddress));
    }

    @ApiOperation("修改收件地址表接口")
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody UserAddress userAddress) {
        if (Objects.isNull(userAddress.getId())) {
            return ResponseResult.fail(ResultCode.CODE_400, "ID不能为空");
        }
        return ResponseResult.success(userAddressService.updateById(userAddress));
    }

    @ApiOperation("删除收件地址表接口")
    @PostMapping("/deleteByIds")
    public ResponseResult<Boolean> deleteByIds(@RequestParam List<Long> ids) {
        return ResponseResult.success(userAddressService.removeByIds(ids));
    }

}
